using System;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Resources;
using PickGold.Charting;

namespace PickGold.Charting.Borders3D
{
	internal class BorderTypeRegistry : IServiceProvider
	{
		private Hashtable _createdBorderTypes = new Hashtable(StringComparer.OrdinalIgnoreCase);
		private System.Resources.ResourceManager _resourceManager;
		internal Hashtable registeredBorderTypes = new Hashtable(StringComparer.OrdinalIgnoreCase);

		public IBorderType GetBorderType(string name)
		{
			if (!this.registeredBorderTypes.Contains(name))
				throw new ArgumentException(SR.ExceptionBorderTypeUnknown(name));
			if (!this._createdBorderTypes.Contains(name))
				this._createdBorderTypes[name] = ((Type)this.registeredBorderTypes[name]).Assembly.CreateInstance(((Type)this.registeredBorderTypes[name]).ToString());
			return (IBorderType)this._createdBorderTypes[name];
		}

		public void Register(string name, Type borderType)
		{
			if (this.registeredBorderTypes.Contains(name))
			{
				if (this.registeredBorderTypes[name].GetType() != borderType)
					throw new ArgumentException(SR.ExceptionBorderTypeNameIsNotUnique(name));
			}
			else
			{
				var flag = false;
				foreach (var type in borderType.GetInterfaces())
				{
					if (type == typeof(IBorderType))
					{
						flag = true;
						break;
					}
				}
				if (!flag)
					throw new ArgumentException(SR.ExceptionBorderTypeHasNoInterface);

				this.registeredBorderTypes[name] = borderType;
			}
		}

		[EditorBrowsable(EditorBrowsableState.Never)]
		object IServiceProvider.GetService(Type serviceType)
		{
			if (serviceType != typeof(BorderTypeRegistry))
				throw new ArgumentException(SR.ExceptionBorderTypeRegistryUnsupportedType(serviceType.ToString()));

			return this;
		}

		public ResourceManager ResourceManager
		{
			get
			{
				if (this._resourceManager == null)
					this._resourceManager = new ResourceManager(typeof(BorderTypeRegistry).Assembly.FullName, Assembly.GetExecutingAssembly());
				return this._resourceManager;
			}
		}
	}
}

